<?php
    namespace app\admin\model;
    use app\common\model\Model;
    use think\Db;
    use think\Exception;

//  model 类
    class UserModel extends Model
    {
        const TABLE_NAME = 'user';
        const CACHE_EXPIRE_LEVEL = self::CACHE_EXPIRE_LEVEL_1;

        protected $name = self::TABLE_NAME;

        // 根据 模糊用户名 返回用户 id
        public static function getUserIdByUserName($name=''){
            if(!$name){
                return [];
            }

            $where['user_name']=['like',"%$name%"];
            $res=self::commonQuery(self::TABLE_NAME,self::CACHE_EXPIRE_LEVEL_3)->where($where)->column('user_id');
            
            return $res?$res:[];

        }
       
        // 根据id  获取用户表字段信息信息
        public static function getUserInfo($user_id){

            return self::commonQuery(self::TABLE_NAME,self::CACHE_EXPIRE_LEVEL)
                 ->where('user_id',$user_id)->find();

        }

        public static function getUserName($user_id)
        {
            $info = self::getUserInfo($user_id);
            return empty($info)?'':$info['user_name'];
        }
        public static function getExtendInfo($user_id,$cache=false)
        {
            return self::commonQuery('userinfo',$cache)
                ->where(['user_id'=>$user_id])
                ->find();
        }

        // 禁用 和恢复客户
        public static function changeStatus($id=null,$status=null){

            return self::commonQuery(self::TABLE_NAME)
                ->where(['user_id'=>$id])
                ->setField('is_del',$status);

        }

        public static function userList($keyword='',$status=''){

            $where = '';
            if($status != 'all'){
                $where['is_del']=$status;
            }

            if ($keyword) {
                if (is_numeric($keyword)) {
                   
                    if(strlen($keyword) == 11)
                    {
                        $where['user_phone']=$keyword;
                    }else{
                        $where['user_id']=$keyword;
                    }
                   
                } else {
                    // 模糊用户名查询
                    $where['user_name']=['like',"%$keyword%"];

                }
            }

            $order='register_time desc';

            return self::commonQuery(self::TABLE_NAME)->where($where)->order($order);

        }


        public static function handleUserInfo($info)
        {
            $user_info = self::getExtendInfo($info['user_id']);
            $info['register_time']=date('Y-m-d H:i:s',$info['register_time']);
            return array_merge($info,$user_info);
        }
        public static function getUserExtendInfoForUpdate($user_id)
        {
            $sql="select user_invite_code from userinfo where user_id = $user_id for UPDATE ";
            $info = self::query($sql);
            return empty($info)?[]:$info[0];

        }
        public static function generateInviteCode($user_id)
        {
            $query = self::commonQuery('userinfo');
            $i=1;
            while(true){
               $rand_num = rand(100000,999999);

                if($i>20)
                {
                    return false;
                }
               try{
                   $query->where(['user_id'=>$user_id])->setField('user_invite_code',$rand_num);
               }catch (\Exception $e)
               {
                   //echo  $e->getMessage();
                   $i++;
                   continue;
               }

               break;
           }
            
            return $rand_num;
        }
        public static function clearInviteCode($user_id)
        {
            return self::commonQuery('userinfo')
                ->where(['user_id'=>$user_id])
                ->setField('user_invite_code','');
        }
    }

